package com.msmall.base.util;

import java.io.File;

public class SimpleFileStrategy extends AbstractFileStrategy {

	@Override
	public String getFileName(){
		return FileIO.getRandomFileName();
	}

	/**
	 * 生成相对路径： file/strings[0]/1../strings[length-1]/<br/>
	 * end with /
	 */
	public String getHomePath(String... strings) {
		StringBuffer buffer = new StringBuffer();
		for(String s:strings){
			if(!StringUtil.isNullOrBlankTrim(s))
				buffer.append("/").append(s);
		}
		String homePath = buffer.toString();
		if(homePath.startsWith(Constants.FILE_SEP))
			homePath = homePath.substring(1);
		if(homePath.length() > 0 && !homePath.endsWith(Constants.FILE_SEP))
			homePath += Constants.FILE_SEP;
		return homePath;
	}

	/**
	 * 如果已经存在则删除
	 */
	public File getFile(String absoluteFile) {
		File target = new File(absoluteFile);
		if (target.exists()) {
			target.delete();
		}else{
			File tFolder = new File(absoluteFile.substring(0, absoluteFile.lastIndexOf("/")));
			if(!tFolder.exists()){
				tFolder.mkdirs();
			}
		}
		return target;
	}

}
